#include <iostream>
#include<algorithm>

using namespace std;

int main() {
    int m;
    cin >> m;
    pair<int, int> list[m];
    int n;
    int right_count[m] = {0};//等会看看dev是否支持这样的写法
    for (int i = 0; i < m; ++i) {
        cin >> list[i].first >> list[i].second;
    }
    sort(list, list + m);
    int pre0[m] = {0};
    int rear1[m+1] = {0};
    for (int i = 1; i < m; i++) {
        if (list[i - 1].second == 0) pre0[i] = pre0[i - 1] + 1;
        else pre0[i] = pre0[i - 1];
    }
    for (int i = m - 1; i >= 0; i--) {
        if (list[i].second == 1) rear1[i] = rear1[i + 1] + 1;
        else rear1[i] = rear1[i + 1];
    }
    right_count[0] = pre0[0]+rear1[0];
    for (int i=1;i<m;i++){
        if (list[i].first == list[i-1].first) continue;
        right_count[i] = pre0[i]+rear1[i];
    }
    int maxValue = *max_element(right_count, right_count + m);
    int tem = 0;
    for (int i = 0; i < m; i++) {
        if (right_count[i] == maxValue && list[i].first > tem) tem = list[i].first;
    }
    cout << tem << endl;
    return 0;
}